Nov. branch merge. Various features backported from stable, various bug fixes.
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?
2 # See deferred.doc
3
4 class LinksUpdate {
5
6 /* private */ var $mId, $mTitle;
7
8 function LinksUpdate( $id, $title )
9 {
10 $this->mId = $id;
11 $this->mTitle = $title;
12 $this->mTitleEnc = wfStrencode( $title );
13 }
14
15
16 function doUpdate()
17 {
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC;
20
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
23
24 if ( $wgEnablePersistentLC ) {
25 // Make sure links cache is regenerated on next load
26 wfQuery("DELETE FROM linkscc WHERE lcc_title = '{$safeTitle}'", DB_WRITE);
27 }
28
29 if ( !$wgUseBetterLinksUpdate ) {
30 $this->doDumbUpdate();
31 return;
32 }
33
34 $fname = "LinksUpdate::doUpdate";
35 wfProfileIn( $fname );
36
37 $del = array();
38 $add = array();
39
40 if( $wgDBtransactions ) {
41 $sql = "BEGIN";
42 wfQuery( $sql, DB_WRITE, $fname );
43 }
44
45 #------------------------------------------------------------------------------
46 # Good links
47
48 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
49 # Delete where necessary
50 if ( count( $del ) ) {
51 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}' AND l_to IN(".
52 implode( ",", $del ) . ")";
53 wfQuery( $sql, DB_WRITE, $fname );
54 }
55 } else {
56 # Delete everything
57 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
58 wfQuery( $sql, DB_WRITE, $fname );
59
60 # Get the addition list
61 $add = $wgLinkCache->getGoodLinks();
62 }
63
64 # Do the insertion
65 $sql = "";
66 if ( 0 != count( $add ) ) {
67 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
68 $first = true;
69 foreach( $add as $lt => $lid ) {
70
71 if ( ! $first ) { $sql .= ","; }
72 $first = false;
73
74 $sql .= "('{$this->mTitleEnc}',{$lid})";
75 }
76 }
77 if ( "" != $sql ) {
78 wfQuery( $sql, DB_WRITE, $fname );
79 }
80
81 #------------------------------------------------------------------------------
82 # Bad links
83
84 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
85 # Delete where necessary
86 if ( count( $del ) ) {
87 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
88 implode( "','", $del ) . "')";
89 wfQuery( $sql, DB_WRITE, $fname );
90 }
91 } else {
92 # Delete all
93 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
94 wfQuery( $sql, DB_WRITE, $fname );
95
96 # Get addition list
97 $add = $wgLinkCache->getBadLinks();
98 }
99
100 # Do additions
101 $sql = "";
102 if ( 0 != count ( $add ) ) {
103 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
104 $first = true;
105 foreach( $add as $blt ) {
106 $blt = wfStrencode( $blt );
107 if ( ! $first ) { $sql .= ","; }
108 $first = false;
109
110 $sql .= "({$this->mId},'{$blt}')";
111 }
112 }
113 if ( "" != $sql ) {
114 wfQuery( $sql, DB_WRITE, $fname );
115 }
116
117 #------------------------------------------------------------------------------
118 # Image links
119 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
120 wfQuery( $sql, DB_WRITE, $fname );
121
122 # Get addition list
123 $add = $wgLinkCache->getImageLinks();
124
125 # Do the insertion
126 $sql = "";
127 $image = Namespace::getImage();
128 if ( 0 != count ( $add ) ) {
129 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
130 $first = true;
131 foreach( $add as $iname => $val ) {
132 # FIXME: Change all this to avoid unnecessary duplication
133 $nt = Title::makeTitle( $image, $iname );
134 $nt->invalidateCache();
135
136 $iname = wfStrencode( $iname );
137 if ( ! $first ) { $sql .= ","; }
138 $first = false;
139
140 $sql .= "('{$this->mTitleEnc}','{$iname}')";
141 }
142 }
143 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
144
145 $this->fixBrokenLinks();
146
147 if( $wgDBtransactions ) {
148 $sql = "COMMIT";
149 wfQuery( $sql, DB_WRITE, $fname );
150 }
151 wfProfileOut( $fname );
152 }
153
154 function doDumbUpdate()
155 {
156 # Old update function. This can probably be removed eventually, if the new one
157 # proves to be stable
158 global $wgLinkCache, $wgDBtransactions;
159 $fname = "LinksUpdate::doDumbUpdate";
160 wfProfileIn( $fname );
161
162 if( $wgDBtransactions ) {
163 $sql = "BEGIN";
164 wfQuery( $sql, DB_WRITE, $fname );
165 }
166
167 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
168 wfQuery( $sql, DB_WRITE, $fname );
169
170 $a = $wgLinkCache->getGoodLinks();
171 $sql = "";
172 if ( 0 != count( $a ) ) {
173 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
174 $first = true;
175 foreach( $a as $lt => $lid ) {
176 if ( ! $first ) { $sql .= ","; }
177 $first = false;
178
179 $sql .= "('{$this->mTitleEnc}',{$lid})";
180 }
181 }
182 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
183
184 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
185 wfQuery( $sql, DB_WRITE, $fname );
186
187 $a = $wgLinkCache->getBadLinks();
188 $sql = "";
189 if ( 0 != count ( $a ) ) {
190 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
191 $first = true;
192 foreach( $a as $blt ) {
193 $blt = wfStrencode( $blt );
194 if ( ! $first ) { $sql .= ","; }
195 $first = false;
196
197 $sql .= "({$this->mId},'{$blt}')";
198 }
199 }
200 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
201
202 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
203 wfQuery( $sql, DB_WRITE, $fname );
204
205 $a = $wgLinkCache->getImageLinks();
206 $sql = "";
207 if ( 0 != count ( $a ) ) {
208 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
209 $first = true;
210 foreach( $a as $iname => $val ) {
211 $iname = wfStrencode( $iname );
212 if ( ! $first ) { $sql .= ","; }
213 $first = false;
214
215 $sql .= "('{$this->mTitleEnc}','{$iname}')";
216 }
217 }
218 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
219
220 $this->fixBrokenLinks();
221
222 if( $wgDBtransactions ) {
223 $sql = "COMMIT";
224 wfQuery( $sql, DB_WRITE, $fname );
225 }
226 wfProfileOut( $fname );
227 }
228
229 function fixBrokenLinks() {
230 /* Update any brokenlinks *to* this page */
231 /* Call for a newly created page, or just to make sure state is consistent */
232
233 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
234 $res = wfQuery( $sql, DB_READ, $fname );
235 if ( 0 == wfNumRows( $res ) ) { return; }
236
237 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
238 $now = wfTimestampNow();
239 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
240 $first = true;
241 while ( $row = wfFetchObject( $res ) ) {
242 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
243 $first = false;
244 $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
245
246 $sql .= "('{$nl}',{$this->mId})";
247 $sql2 .= $row->bl_from;
248 }
249 $sql2 .= ")";
250 wfQuery( $sql, DB_WRITE, $fname );
251 wfQuery( $sql2, DB_WRITE, $fname );
252
253 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
254 wfQuery( $sql, DB_WRITE, $fname );
255 }
256
257 }
258
259 ?>